package contacts;

import files.FileHelper;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Jaan
 * Date: 19.11.11
 * Time: 22:13
 */
public class ContactHelper {
  public ArrayList<Contact> myContacts = new ArrayList<Contact>();
  private static org.apache.log4j.Logger log = utils.Logger.getInstance(ContactHelper.class.getName());

  public void setFilePathAndName(String filePathAndName) {
    this.filePathAndName = filePathAndName;
  }

  public String getFilePathAndName() {
    return filePathAndName;
  }

  String filePathAndName;
  FileHelper fileHelper;

  public FileHelper getFileHelper() {
    return fileHelper;
  }

  public ArrayList<Contact> getMyContacts(){
    return myContacts;
  }

  public ContactHelper() {
    fileHelper = new FileHelper();
  }

  public void loadContacts() {
    String contactInfo = fileHelper.readFromFile(filePathAndName);
    if (contactInfo == null || contactInfo.equals("")) {
      log.info("No contacts found in my contact list");
    } else {
      myContacts = parseContacts(contactInfo);
    }
  }

  public ArrayList<Contact> parseContacts(String contactInfo) {
    String[] contacts;
    ArrayList<Contact> contactList = new ArrayList<Contact>();
    try {
      contactInfo = contactInfo.replaceAll("[,\n\\[\\]]", "").replaceAll("\"\"", "\"").trim();
      contacts = contactInfo.split("\"");
      for (int i = 1; i < contacts.length; i += 2) {
        contactList.add(new Contact(contacts[i], contacts[i + 1]));
      }
    } catch (Exception e) {
      log.warn("Corrupted contacts file.");
    }
    return contactList;
  }


  public Contact getContact(String name) {
    for (Contact contact : myContacts) {
      if (contact.getName().equals(name)) return contact;
    }
    return null;
  }

  public Contact getContact(String name, String ipAndPort) {
    for (Contact contact : myContacts) {
      if (contact.getName().equals(name) && contact.getIPAndPort().equals(ipAndPort)) return contact;
    }
    return null;
  }


  public void addContact(Contact contact) {
    myContacts.add(contact);
    saveContacts(myContacts);
  }

  public void saveContacts(ArrayList<Contact> contacts) {
    String contents = "[\n";
    for (int i = 0; i < contacts.size(); i++) {
      contents += "[\"" + contacts.get(i).getName() + "\",\"" + contacts.get(i).getIPAndPort() + "\"]" + (((i + 1) == contacts.size()) ? "\n" : ",\n");
    }
    contents += "]";
    fileHelper.writeToFile(filePathAndName, contents);
  }

  public void saveContacts() {
    saveContacts(getMyContacts());
  }

  public String getContactNameByIp(String ip) {
    for (int i = myContacts.size() - 1; i > -1; i--) {
        if(myContacts.get(i).getIPAndPort().equals(ip))
          return myContacts.get(i).getName();
    }
//    for (Contact contact : myContacts) {
//      if(contact.getIPAndPort().equals(ip)) return contact.getName();
//    }
    return "Unknown";
  }
}
